//获取用户信息
var member = member()
//退出登录状态
if (member) {
//欢迎
    $('.userName').html(member.phone.substring(0, 3) + "******" + member.phone.substring(9, 11))
    //我要还款
    $('#wlhk').on('click',function () {
       window.location.href='/toMain'
    })
} else {
    $('.login').css('display', 'none')
    $('.loginOut').css('display', 'block')
    //我要还款
$('#wlhk').on('click',function () {
    layer.msg('请先登录!', {time: 3000,icon:2})
})
}
$(function(){
    changPhonecode()
})

layui.use('form', function () {
    var form = layui.form;
    //登录
    $('.enter').on('click',function () {
        layer.open({
            type: 1,
            content: $('#loginOpen'),
            area: ['500px', '300px'],
            title: ['登录', 'font-size:18px;']
        });

        form.on('submit(loginSend)', function(data){
            $.post('/login', data.field,function (data) {
                if(data.code==10004){
                    window.location.href='/toMain'
                }else{
                    layer.msg(data.message, {time: 3000})
                    $('#loginOpen')[0].reset()
                }
            })
            return false;
        });
    })

    //忘记密码
    $('#forget').on('click',function () {
        layer.closeAll()
        layer.open({
            type: 1,
            content: $('#forgetOpen'),
            area: ['600px', '400px'],
            title: ['忘记密码', 'font-size:18px;']
        });
        form.on('submit(forgetSend)', function(data){
            $.post('/retrievePwd', data.field, function (res) {
                if (res.code == 10003) {
                    layer.closeAll()
                    layer.open({
                        type: 1,
                        content: $('#loginOpen'),
                        area: ['500px', '300px'],
                        title: ['登录', 'font-size:18px;']
                    });
                    layer.msg("密码修改成功!请登录", {time: 3000})
                } else if (res.code == 20010) {
                    layer.msg("您的手机号尚未注册!", {time: 3000})
                } else if (res.code == 20006) {
                    layer.msg("请输入正确的短信验证码!", {time: 3000})
                }
            })
            return false;
        });
    })
});

function changPhonecode() {
    //修改密码发送短信验证码
    $('#phoneSms').one('click', function () {
        this.innerHTML = '60秒后重新发送'
        countDown($(this))
        $.post('/sendSms', {'phone':$('#userPhone').val()}, function (data) {
            if (data.code != 10000) {
                layer.msg(data.message, {time: 3000})
            }
        })
    })
}

//60秒倒计时s
function countDown(dom) {
    var timer, k = 60,
        i = 0
    timer = setInterval(function () {
        i++;
        dom.html((k - i) + '秒后重新发送')
        if ((k - i) == 0) {
            dom.html('发送验证码')
            changPhonecode()
            clearInterval(timer)
        }
    }, 1000)
}
var mySwiper = new Swiper ('.swiper-container', {
    loop: true,
    autoplay:2000,
    pagination: '.swiper-pagination'
})